package com.zhongli.devplatform.controller.sys;

import com.zhongli.devplatform.dto.sys.SystemParameterDTO;
import com.zhongli.devplatform.query.sys.SystemParameterQuery;
import com.zhongli.devplatform.service.imp.sys.ReadSystemParameterService;
import com.zhongli.devplatform.bean.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/systemParameter")
@RestController
public class SystemParameterController {

    @Autowired
    private ReadSystemParameterService systemParameterService;

    @Autowired
    private CacheManager cacheManager;

    @Value("${dev-platform.cache-name}")
    private String cacheName;

    @PostMapping("/listPage")
    public Res listPage(SystemParameterQuery query) {
        return new Res(systemParameterService.listPage(query));
    }

    @PostMapping("/save")
    public Res save(SystemParameterDTO SystemParameterDTO) {
        systemParameterService.save(SystemParameterDTO);
        return new Res(null);
    }

    @PostMapping("/delete")
    public Res delete(@RequestBody List<Long> ids) {
        systemParameterService.delete(ids);
        return new Res(null);
    }

    @RequestMapping("/isExist")
    public Res isExist(SystemParameterQuery query) {
        return new Res(systemParameterService.isExist(query));
    }

    @RequestMapping("/refreshCache")
    public Res refreshCache() {
        systemParameterService.putCache();
        return new Res(null);
    }

    @RequestMapping("/getParamValue")
    public Res getParamValue(String paramKey) {
        String paramValue = cacheManager.getCache(cacheName + ReadSystemParameterService.CACHE_KEY).get(paramKey, String::new);
        return new Res(paramValue);
    }


}
